#include "StdAfx.h"
#include "VDialog.h"

// VDialog ////////////////////////////////////////////////////////////////////

//
VDialog::VDialog(QWidget *parent)
	:QDialog(parent)
	,m_acceptButton(NULL)
	,m_rejectButton(NULL)
{
	m_vBoxLayout = new QVBoxLayout;
	
	m_buttonBox = new QDialogButtonBox(Qt::Horizontal);
		m_vBoxLayout->addWidget(new QFrame);
		m_vBoxLayout->addWidget(m_buttonBox);
	
	connect(
		m_buttonBox,SIGNAL( clicked(QAbstractButton *) ),
		this,SLOT( clicked(QAbstractButton *) )
	);
	
	setLayout(m_vBoxLayout);
}

//
void VDialog::addTopLayout(QLayout *layout)
{
	m_vBoxLayout->insertLayout(0,layout);
}

//
void VDialog::addButton(QPushButton *button,EButtonRole role)
{
	m_buttonBox->addButton(button,QDialogButtonBox::ActionRole);
	
	if (role == AcceptRole) {
		m_acceptButton = button;
	}
	else if (role == RejectRole) {
		m_rejectButton = button;
	}
}

//
bool VDialog::exec(QAction *action) 
{ 
	if (action) {
		QList<QWidget*> wList = action->associatedWidgets();
		QWidget *widget = NULL;

		foreach (QWidget *p,wList) {
			if (qobject_cast<QToolButton*>(p)) {
				widget = p;
				break;
			}
		}

		if (widget) {
			QPoint pt = widget->mapToGlobal(widget->geometry().bottomLeft());
			this->move(pt);
		}
	}


	return QDialog::exec() == QDialog::Accepted;
}

//
void VDialog::clicked(QAbstractButton *button)
{
	if (button == m_acceptButton) {
		accept();
	}
	else if (button == m_rejectButton) {
		reject();
	}
}
